找传奇、传世资源到传世资源站!

android天气预报实现定位与天气报道_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

此项目实现了定位功能,可以查询今天及未来四天的天气情况,采用了一个二级联动的城市选择器

android天气预报实现定位与天气报道_ Android平台开发-第1张android天气预报实现定位与天气报道_ Android平台开发-第2张android天气预报实现定位与天气报道_ Android平台开发-第3张

mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
mLocationClient.registerLocationListener(myListener); // 注册监听函数
initLocation();
// 开始定位
mLocationClient.start();

}

/*@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Intent intent = getIntent();
if(!"".equals(intent.getStringExtra("cityname"))){
requestJuHeData(intent.getStringExtra("cityname"));
}
}*/

private void initLocation() {
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("bd09ll");// 可选,默认gcj02,设置返回的定位结果坐标系
option.setScanSpan(0);// 可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);// 可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);// 可选,默认false,设置是否使用gps
option.setLocationNotify(true);// 可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
option.setIsNeedLocationDescribe(true);// 可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
option.setIsNeedLocationPoiList(true);// 可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
option.setIgnoreKillProcess(false);// 可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.SetIgnoreCacheException(false);// 可选,默认false,设置是否收集CRASH信息,默认收集
option.setEnableSimulateGps(false);// 可选,默认false,设置是否需要过滤gps仿真结果,默认需要
mLocationClient.setLocOption(option);
}

// 关联控件
public void initWdget() {
tv_city = (TextView) findViewById(R.id.tv_city);
tv_temp = (TextView) findViewById(R.id.tv_temp);
tv_wind = (TextView) findViewById(R.id.tv_wind);
tv_date = (TextView) findViewById(R.id.tv_date);
seCity = (Button) findViewById(R.id.seCity);
today = (TextView) findViewById(R.id.today);
today1 = (TextView) findViewById(R.id.today1);
today2 = (TextView) findViewById(R.id.today2);
tomorrow = (TextView) findViewById(R.id.tomorrow);
tomorrow1 = (TextView) findViewById(R.id.tomorrow1);
tomorrow2 = (TextView) findViewById(R.id.tomorrow2);
nextto = (TextView) findViewById(R.id.nextto);
nextto1 = (TextView) findViewById(R.id.nextto1);
nextto2 = (TextView) findViewById(R.id.nextto2);
nexttoo = (TextView) findViewById(R.id.nexttoo);
nexttoo1 = (TextView) findViewById(R.id.nexttoo1);
nexttoo2 = (TextView) findViewById(R.id.nexttoo2);
nexttooo = (TextView) findViewById(R.id.nexttooo);
nexttooo1 = (TextView) findViewById(R.id.nexttooo1);
nexttooo2 = (TextView) findViewById(R.id.nexttooo2);
tv_hint = (TextView)findViewById(R.id.tv_hint);
        yundong = (TextView)findViewById(R.id.yundong);
        yundongindex = (TextView)findViewById(R.id.yundongindex);
        xiche = (TextView)findViewById(R.id.xiche);
        xicheindex = (TextView)findViewById(R.id.xicheindex);
        ziwaixian = (TextView)findViewById(R.id.ziwaixian);
        ziwaixianindex = (TextView)findViewById(R.id.ziwaixianindex);
        guangjie = (TextView)findViewById(R.id.guangjie);
        guangjieindex = (TextView)findViewById(R.id.guangjieindex);
        fangshai = (TextView)findViewById(R.id.fangshai);
        fangshaiindex = (TextView)findViewById(R.id.fangshaiindex);
        diaoyu = (TextView)findViewById(R.id.diaoyu);
        diaoyuindex = (TextView)findViewById(R.id.diaoyuindex);
        chuanyi = (TextView)findViewById(R.id.chuanyi);
        chuanyiindex = (TextView)findViewById(R.id.chuanyiindex);
        shushi = (TextView)findViewById(R.id.shushi);
        shushiindex = (TextView)findViewById(R.id.shushiindex);
        
        
}

public void setImage(TextView tv, String weather) {
if ("阴转多云".equals(weather)) {
tv.setBackgroundResource(R.drawable.yinzhuanduoyun);

} else if ("晴".equals(weather)) {
tv.setBackgroundResource(R.drawable.qing);
} else if ("多云".equals(weather)) {
tv.setBackgroundResource(R.drawable.duoyun);
} else if ("雷阵雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.leizhenyu);
} else if ("小雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.xiaoyu);
} else if ("中雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.zhongyu);
} else if ("暴雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.baoyu);
} else if ("大雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.dayu);
} else if ("阵雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.zhenyu);
} else if ("中雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.zhongyu);
}else if ("阴转阵雨".equals(weather)) {
tv.setBackgroundResource(R.drawable.yinzhuanzhenyu);
}else if ("晴转阴".equals(weather)) {
tv.setBackgroundResource(R.drawable.qingzhuanyin);
}
}

public void requestJuHeData(String cityname) {
Parameters param = new Parameters();
param.add("cityname", cityname);
param.add("dtype", "json");
JuheData.executeWithAPI(MainActivity.this, // 上下文对象
39, // 数据ID
"http://v.juhe.cn/weather/index",// 接口地址
JuheData.GET,// 请求方法
param,// 传入参数
new DataCallBack() {
// 请求失败后执行
@Override
public void onFailure(int arg0, String arg1, Throwable arg2) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, arg2.toString(),
Toast.LENGTH_SHORT).show();
}

// 不管成功与否,都执行该方法
@Override
public void onFinish() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "finish",
Toast.LENGTH_SHORT).show();
}

// 请求成功后执行
@Override
public void onSuccess(int arg0, String arg1) {
// TODO Auto-generated method stub
try {
JSONObject json = new JSONObject(arg1);
String resultcode = json.getString("resultcode");
if ("200".equals(resultcode)) {
JSONObject result = json
.getJSONObject("result");
JSONObject jstoday = result
.getJSONObject("today");
JSONObject sk = result.getJSONObject("sk");
JSONObject future = result
.getJSONObject("future");
String city = jstoday.getString("city");
String temp = sk.getString("temp");
String wind = jstoday.getString("wind");
String date = jstoday.getString("date_y");
String reg = "[\u4e00-\u9fa5]";
Pattern pat = Pattern.compile(reg);
Matcher mat = pat.matcher(date);
todayDate = mat.replaceAll("");
tv_city.setText(city);
tv_temp.setText(temp "℃");
tv_wind.setText(wind);
tv_date.setText(date);
today.setText("今天");
String todaywe = jstoday.getString("weather");
setImage(today1,todaywe);
today2.setText(jstoday.getString("temperature"));
tomorrow.setText("明天");
String tomorrowDate = (Integer.parseInt(todayDate) 1) "";
JSONObject tomorrow = future
.getJSONObject("day_" tomorrowDate);
String tomorrowwe = tomorrow.getString("weather");
setImage(tomorrow1,tomorrowwe);
tomorrow2.setText(tomorrow.getString("temperature"));
String nextDate = (Integer.parseInt(todayDate) 2) "";
JSONObject nextday = future
.getJSONObject("day_" nextDate);
String nextdaywe = nextday.getString("weather");
nextto.setText("后天");
setImage(nextto1,nextdaywe);
nextto2.setText(nextday.getString("temperature"));
String nexttooDate = (Integer.parseInt(todayDate) 3) "";
JSONObject nexttooday = future
.getJSONObject("day_" nexttooDate);
nexttoo.setText(nexttooday.getString("week"));
String nexttoowe = nexttooday.getString("weather");
setImage(nexttoo1,nexttoowe);
nexttoo2.setText(nexttooday.getString("temperature"));
String nexttoooDate = (Integer.parseInt(todayDate) 4) "";
JSONObject nexttoooday = future
.getJSONObject("day_" nexttoooDate);
nexttooo.setText(nexttoooday.getString("week"));
String nexttooowe = nexttoooday.getString("weather");
setImage(nexttooo1,nexttooowe);
nexttooo2.setText(nexttoooday.getString("temperature"));
tv_hint.setText(jstoday.getString("dressing_advice"));
yundong.setBackgroundResource(R.drawable.yundong);
yundongindex.setText(jstoday.getString("exercise_index"));
xiche.setBackgroundResource(R.drawable.xiche);
xicheindex.setText(jstoday.getString("wash_index"));
ziwaixian.setBackgroundResource(R.drawable.ziwaixian);
ziwaixianindex.setText(jstoday.getString("uv_index"));
guangjie.setBackgroundResource(R.drawable.guangjie);
guangjieindex.setText(jstoday.getString("travel_index"));
fangshai.setBackgroundResource(R.drawable.fangshai);
fangshaiindex.setText("较" jstoday.getString("uv_index"));
diaoyu.setBackgroundResource(R.drawable.diaoyu);
diaoyuindex.setText(jstoday.getString("exercise_index"));
chuanyi.setBackgroundResource(R.drawable.chuanyi);
chuanyiindex.setText(jstoday.getString("dressing_index"));
shushi.setBackgroundResource(R.drawable.shushi);
shushiindex.setText("较舒适");
}

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

/*
* Toast.makeText( MainActivity.this,
* arg1,Toast.LENGTH_SHORT). show();
*/
Log.i("this", arg1);
}

});
;
}

public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
// Receive Location
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
sb.append("\nspeed : ");
sb.append(location.getSpeed());// 单位:公里每小时
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\nheight : ");
sb.append(location.getAltitude());// 单位:米
sb.append("\ndirection : ");
sb.append(location.getDirection());// 单位度
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append("\ndescribe : ");
sb.append("gps定位成功");

} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
sb.append("\naddr : ");
sb.append(location.getAddrStr());
// 运营商信息
sb.append("\noperationers : ");
sb.append(location.getOperators());
sb.append("\ndescribe : ");
sb.append("网络定位成功");
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
sb.append("\ndescribe : ");
sb.append("离线定位成功,离线定位结果也是有效的");
} else if (location.getLocType() == BDLocation.TypeServerError) {
sb.append("\ndescribe : ");
sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("网络不同导致定位失败,请检查网络是否通畅");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
}
sb.append("\nlocationdescribe : ");
sb.append(location.getLocationDescribe());// 位置语义化信息
List<Poi> list = location.getPoiList();// POI数据
if (list != null) {
sb.append("\npoilist size = : ");
sb.append(list.size());
for (Poi p : list) {
sb.append("\npoi= : ");
sb.append(p.getId() " " p.getName() " " p.getRank());
}
}
Log.i("tianqi", sb.toString());
city = location.getCity().toString()
.substring(0, location.getCity().indexOf("市"));
Toast.makeText(MainActivity.this, location.getCity(),
Toast.LENGTH_SHORT).show();

Intent intent = getIntent();
if(!"".equals(intent.getStringExtra("cityname")) && intent.getStringExtra("cityname")!=null){
requestJuHeData(intent.getStringExtra("cityname"));
/*Toast.makeText(MainActivity.this, intent.getStringExtra("cityname") "456", Toast.LENGTH_SHORT).show();*/
}else{
requestJuHeData(city);
/*Toast.makeText(MainActivity.this, city "123", Toast.LENGTH_SHORT).show();*/
}
}
}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复